** 	Replication .do file for                                                                   **
** 	Byers & Shay "The Electoral Connection, State AG, and the Dynamics of Incarceration Rates" ** 	
**	Journal of Public Policy                                                        	       **

* This code, if run properly, replicates Tables 1-5 and Figures 1-2*
* Please cite the above article if you use these data or code *
* All the analyses were done using Stata (Version 16).*

* Set working directory to load the data: Note that users will need to input the relevant file path
		cd "/Users/user/Desktop"
		import delimited "Byers_and_Shay_JPP.csv", clear

		
*****************************
*Main Text Analysis
******************************

*Declare Dataset and Panel ID
xtset fip year

*Table 1
xtreg dv i.year urban leg c.citizen govt_ideo c.crime##c.elected  law_enforcement_spending south  percentblack povrate gsp income state_revenue, mle
estat ic

*Figure 1
gen pipe = "|"
gen where = 140
margins, at(crime = (78 (50) 828) elected==(1)) atmeans

marginsplot,  recast(line) recastci(rarea) legend(off) ytitle("Predicted Incarceration Rate", margin(r=2)) xtitle("Crime Rate", margin(t=2)) title("Elected Attorney Generals") /// 
addplot(scatter where crime if e(sample) & elected == 1, ms(none) mlabel(pipe) mlabpos(0) xlabel(78(50)828) ylabel(150(50)550, angle(0)))

*Figure 2
margins, at(crime = (78 (50) 828) elected == (0)) atmeans

marginsplot, recast(line) recastci(rarea) legend(off) ytitle("Predicted Incarceration Rate", margin(r=2)) xtitle("Crime Rate", margin(t=2)) title("Non-Elected Attorney Generals") /// 
addplot(scatter where crime if e(sample) & elected == 0, ms(none) mlabel(pipe) mlabpos(0) xlabel(78(50)828) ylabel(150(50)550, angle(0)))  

*Table 2
xtreg police_percent i.year urban leg c.citizen govt_ideo c.crime##c.elected  law_enforcement_spending south  percentblack povrate gsp income state_revenue, mle

estat ic

*Table 3
xtreg dv i.year urban leg c.citizen govt_ideo c.crime##dem_elected c.crime##gop_elected  law_enforcement_spending south  percentblack povrate gsp income state_revenue, mle

estat ic

*Table 4
xtreg dv i.year urban leg c.citizen leg_gop##gop_elected  leg_dem##gop_elected leg_gop##dem_elected  leg_dem##dem_elected c.crime gop_elected##dem_gov  dem_elected##dem_gov  law_enforcement_spending south  percentblack povrate gsp income state_revenue, mle

estat ic

*Table 5
xtreg dv i.year urban leg  govt_ideo c.crime c.citizen##c.elected  law_enforcement_spending south  percentblack povrate gsp income state_revenue, mle

estat ic



